package com.jzo2o.customer.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.AgencyCertificationAuditMapper;
import com.jzo2o.customer.mapper.AgencyCertificationMapper;
import com.jzo2o.customer.model.domain.AgencyCertification;
import com.jzo2o.customer.model.dto.AgencyCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.response.RejectReasonResDTO;
import com.jzo2o.customer.service.IAgencyCertificationService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.customer.model.domain.AgencyCertificationAudit;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * <p>
 * 机构认证信息表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-06
 */
@Service
public class AgencyCertificationServiceImpl extends ServiceImpl<AgencyCertificationMapper, AgencyCertification> implements IAgencyCertificationService {
    @Resource
    private AgencyCertificationAuditMapper agencyCertificationAuditMapper;

    /**
     * 根据机构id更新
     *
     * @param agencyCertificationUpdateDTO 机构认证更新模型
     */
    @Override
    public void updateByServeProviderId(AgencyCertificationUpdateDTO agencyCertificationUpdateDTO) {
        LambdaUpdateWrapper<AgencyCertification> updateWrapper = Wrappers.<AgencyCertification>lambdaUpdate()
                .eq(AgencyCertification::getId, agencyCertificationUpdateDTO.getId())
                .set(AgencyCertification::getCertificationStatus, agencyCertificationUpdateDTO.getCertificationStatus())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getName()), AgencyCertification::getName, agencyCertificationUpdateDTO.getName())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getIdNumber()), AgencyCertification::getIdNumber, agencyCertificationUpdateDTO.getIdNumber())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getLegalPersonName()), AgencyCertification::getLegalPersonName, agencyCertificationUpdateDTO.getLegalPersonName())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getLegalPersonIdCardNo()), AgencyCertification::getLegalPersonIdCardNo, agencyCertificationUpdateDTO.getLegalPersonIdCardNo())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getBusinessLicense()), AgencyCertification::getBusinessLicense, agencyCertificationUpdateDTO.getBusinessLicense())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getCertificationTime()), AgencyCertification::getCertificationTime, agencyCertificationUpdateDTO.getCertificationTime());
        super.update(updateWrapper);
    }

    @Override
    public void submit(AgencyCertificationAuditAddReqDTO agencyCertificationAuditAddReqDTO) {
        AgencyCertificationAudit bean =
                BeanUtils.toBean(agencyCertificationAuditAddReqDTO, AgencyCertificationAudit.class);
        //待审核
        bean.setAuditStatus(0);
        agencyCertificationAuditMapper.insert(bean);
//        认证中或者认证过的的会在认证表中有记录
        LambdaQueryWrapper<AgencyCertification> agencyCertificationLambdaQueryWrapper
                = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<AgencyCertification> query = agencyCertificationLambdaQueryWrapper.eq(AgencyCertification::getId,
                agencyCertificationAuditAddReqDTO.getServeProviderId());

        AgencyCertification agencyCertification = baseMapper.selectOne(query);

        if (!(Objects.isNull(agencyCertification))) {
            //2.将认证信息状态更新为认证中
            agencyCertification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());//认证中
            baseMapper.updateById(agencyCertification);

        }else
        {
            AgencyCertification agencyCertification1 = new AgencyCertification();
            agencyCertification1.setId(agencyCertificationAuditAddReqDTO.getServeProviderId());
            agencyCertification1.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
            baseMapper.insert(agencyCertification1);

        }


    }

    @Override
    public RejectReasonResDTO lookRejectReason() {
        Long id = UserContext.currentUser().getId();
        LambdaQueryWrapper<AgencyCertificationAudit> agencyCertificationAuditLambdaQueryWrapper
                = new LambdaQueryWrapper<>();
        //注意服务端和机构端会请求多个机构认证信息，所以这里要限制
        LambdaQueryWrapper<AgencyCertificationAudit> query = agencyCertificationAuditLambdaQueryWrapper
                .eq(AgencyCertificationAudit::getServeProviderId, id)
                .eq(AgencyCertificationAudit::getCertificationStatus, 3)
//                注意这个写法，注意这里要写limit 1
                .orderByDesc(AgencyCertificationAudit::getCreateTime)
                .last("limit 1");

        AgencyCertificationAudit agencyCertificationAudit =
                agencyCertificationAuditMapper.selectOne(query);

        return new RejectReasonResDTO(agencyCertificationAudit.getRejectReason());

    }


}
